package cn.java89.userconsumer.service;

import cn.java89.entity.AjaxResult;
import cn.java89.entity.Page;
import cn.java89.entity.Student;
import cn.java89.service.IStudentService;
import cn.java89.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author oldliu
 * @since 1.0
 */
@Service
public class StudentService implements IStudentService {
    @Autowired
    private RestTemplate restTemplate;
    //3.0


    @Override
    public AjaxResult get(Long stuno) {

        // url = String.format("http://%s:%d/student/get/" + stuno, host, port);
        String url = "http://userservice/student/get/" + stuno;


        return restTemplate.getForObject(url, AjaxResult.class);
    }

    @Override
    public AjaxResult login(String phone, String pass) {


        String url = "http://userservice/student/login";
        MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
        formData.add("phone", phone);
        formData.add("pass", pass);

        return restTemplate.postForObject(url, formData, AjaxResult.class);
    }

    @Override
    public Page search(int pageNo, int pageSize, String sname, String qq,
                       String phone, Date enterStart,
                       Date enterEnd) {

        String url = "http://userservice/student/search";
        Map<String, Object> params = new HashMap<>();
        params.put("pageNo", pageNo);
        params.put("pageSize", pageSize);
        params.put("sname", StrUtil.toStr(sname));
        params.put("qq", StrUtil.toStr(qq));
        params.put("phone", StrUtil.toStr(phone));
        params.put("enterStart", StrUtil.toStr(enterStart));
        params.put("enterEnd", StrUtil.toStr(enterEnd));
        return restTemplate.getForObject(url, Page.class, params);
    }

//    @Autowired
//    private DiscoveryClient client;
//
//    //2.0
//    @Override
//    public AjaxResult get(Long stuno) {
//        List<ServiceInstance> instances = client.getInstances("userservice");
//        ServiceInstance u = instances.get((int) (instances.size() * Math.random()));
//        String host = u.getHost();
//        int port = u.getPort();
//        // url = String.format("http://%s:%d/student/get/" + stuno, host, port);
//        String url = "http://" + host + ":" + port + "/student/get/" + stuno;
//
//
//        return restTemplate.getForObject(url, AjaxResult.class);
//    }
//
//    @Override
//    public AjaxResult login(String phone, String pass) {
//        List<ServiceInstance> instances = client.getInstances("userservice");
//        ServiceInstance u = instances.get((int) (instances.size() * Math.random()));
//        String host = u.getHost();
//        int port = u.getPort();
//
//        String url = "http://" + host + ":" + port + "/student/login";
//        MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
//        formData.add("phone", phone);
//        formData.add("pass", pass);
//
//        return restTemplate.postForObject(url, formData, AjaxResult.class);
//    }
//
//    @Override
//    public Page search(int pageNo, int pageSize, String sname, String qq,
//                       String phone, Date enterStart,
//                       Date enterEnd) {
//        List<ServiceInstance> instances = client.getInstances("userservice");
//        ServiceInstance u = instances.get((int) (instances.size() * Math.random()));
//        String host = u.getHost();
//        int port = u.getPort();
//
//        String url = "http://" + host + ":" + port + "/student/search";
//        Map<String, Object> params = new HashMap<>();
//        params.put("pageNo", pageNo);
//        params.put("pageSize", pageSize);
//        params.put("sname", StrUtil.toStr(sname));
//        params.put("qq", StrUtil.toStr(qq));
//        params.put("phone", StrUtil.toStr(phone));
//        params.put("enterStart", StrUtil.toStr(enterStart));
//        params.put("enterEnd", StrUtil.toStr(enterEnd));
//        return restTemplate.getForObject(url, Page.class, params);
//    }

    //1.0
//    @Override
//    public AjaxResult get(Long stuno) {
//        String url = "http://localhost:9999/student/get/" + stuno;
//        return restTemplate.getForObject(url, AjaxResult.class);
//    }
//
//    @Override
//    public AjaxResult login(String phone, String pass) {
//        //System.out.println("!!!!!!" + phone + "," + pass);
//        String url = "http://localhost:9999/student/login";
//        MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
//        formData.add("phone", phone);
//        formData.add("pass", pass);
//
//        return restTemplate.postForObject(url, formData, AjaxResult.class);
//    }
//
//    @Override
//    public Page search(int pageNo, int pageSize, String sname, String qq,
//                       String phone, Date enterStart,
//                       Date enterEnd) {
//        String url = "http://localhost:9999/student/search";
//        Map<String, Object> params = new HashMap<>();
//        params.put("pageNo", pageNo);
//        params.put("pageSize", pageSize);
//        params.put("sname", StrUtil.toStr(sname));
//        params.put("qq", StrUtil.toStr(qq));
//        params.put("phone", StrUtil.toStr(phone));
//        params.put("enterStart", StrUtil.toStr(enterStart));
//        params.put("enterEnd", StrUtil.toStr(enterEnd));
//        return restTemplate.getForObject(url, Page.class, params);
//    }

}